﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.FeedbackModule.Entity;

namespace Ivy.FeedbackModule.DataAccess
{
    public class FeedbackUserDao : IDaoInterface<FeedbackUser, FeedbackUserCriteria>
    {
        #region Variables & Constants
        #endregion Variables & Constants

        #region Constructor & Destructor
        public FeedbackUserDao() : base() { }
        //public FeedbackUserDao(FeedbackUser FeedbackUser)
        //{
        //    this.FeedbackUserKey = FeedbackUser.FeedbackUserKey;
        //    this.FeedbackUserName = FeedbackUser.FeedbackUserName;
        //    this.FeedbackUserEmail = FeedbackUser.FeedbackUserEmail;
        //    this.RegisteredUserkey = FeedbackUser.RegisteredUserkey;

        //    this.Sys_CreatedByID = FeedbackUser.Sys_CreatedByID;
        //    this.Sys_DateOfCreation = FeedbackUser.Sys_DateOfCreation;
        //    this.Sys_DateOfModification = FeedbackUser.Sys_DateOfModification;
        //    this.Sys_DeleteType = FeedbackUser.Sys_DeleteType;
        //    this.Sys_InitialPrimaryKeys = FeedbackUser.Sys_InitialPrimaryKeys;
        //    this.Sys_IsDeleted = FeedbackUser.Sys_IsDeleted;
        //    this.Sys_ModifiedByID = FeedbackUser.Sys_ModifiedByID;
        //    this.Sys_OrganizationID = FeedbackUser.Sys_OrganizationID;
        //    this.Sys_WorkstationName = FeedbackUser.Sys_WorkstationName;
        //    this.EntityState = FeedbackUser.EntityState;



        //}
        #endregion Constructor & Destructor

        #region Public Methods
        ///<summary>
        /// Created by Ali BALCI
        /// </summary>
        /// <returns></returns>
        #region  IDaoInterface<FeedbackUser,FeedbackCriteria> Members

        public bool Insert(FeedbackUser feedbackUserEntity)
        {
            bool IsOperationSuccessful = false;
            try
            {
                if (feedbackUserEntity.EntityState == EntityState.New)
                {
                    DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                    DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                    CmdObj.CommandText = "Ivy_FeedbackUser_Insert";
                    CmdObj.CommandType = CommandType.StoredProcedure;

                    DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackUserName", ParameterDirection.Input, DbType.String, feedbackUserEntity.FeedbackUserName);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackUserEmail", ParameterDirection.Input, DbType.String, feedbackUserEntity.FeedbackUserEmail);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@RegisteredUserKey", ParameterDirection.Input, DbType.Int32, (int)feedbackUserEntity.RegisteredUserkey);

                    DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, feedbackUserEntity);

                    DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackUserKey", ParameterDirection.Output, DbType.Int32, DBNull.Value);

                    CmdObj.Connection = DbConn;
                    DbConn.Open();
                    CmdObj.Prepare();
                    int _NoOfRecords = CmdObj.ExecuteNonQuery();
                    if (_NoOfRecords == 1)
                    {
                        feedbackUserEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                        feedbackUserEntity.FeedbackUserKey = Convert.ToInt32(CmdObj.Parameters["@FeedbackUserKey"].Value);
                        feedbackUserEntity.EntityState = EntityState.UnChanged;
                        IsOperationSuccessful = true;
                    }
                    CmdObj.Dispose();
                    DbConn.Dispose();
                }
                else
                {
                    throw new Exception("For inserting new Feedback entity data to database,[EntityState] attribute should be [New]");
                }
            }
            catch (Exception ex)
            {

                System.Diagnostics.Debug.Print(ex.Message);
                throw;
            }
            return IsOperationSuccessful;
        }
        public bool Update(FeedbackUser feedbackUserEntity)
        {
            bool IsOperationSuccessful = false;
            try
            {
                if (feedbackUserEntity.EntityState == EntityState.Changed)
                {
                    DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                    DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                    CmdObj.CommandText = "Ivy_FeedBackUser_Update";
                    CmdObj.CommandType = CommandType.StoredProcedure;

                    DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackUserKey", ParameterDirection.Input, DbType.Int32, feedbackUserEntity.FeedbackUserKey);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackUserName", ParameterDirection.Input, DbType.String, feedbackUserEntity.FeedbackUserName);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackUserEmail", ParameterDirection.Input, DbType.String, feedbackUserEntity.FeedbackUserEmail);

                    DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, feedbackUserEntity);
                    DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);
                    CmdObj.Connection = DbConn;
                    DbConn.Open();
                    CmdObj.Prepare();
                    int _NoOfRecords = CmdObj.ExecuteNonQuery();
                    if (_NoOfRecords == 1)
                    {
                        feedbackUserEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                        feedbackUserEntity.EntityState = EntityState.UnChanged;
                        IsOperationSuccessful = true;
                    }

                    CmdObj.Dispose();
                    DbConn.Close();
                }
                else
                {
                    throw new Exception("For updating existing Feedback entity data to database, [EntityState] attribute should be [Changed]");
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw;
            }

            return IsOperationSuccessful;
        }
        public bool Delete(FeedbackUser feedbackUserEntity)
        {
            bool IsOperationSuccessfull = false;
            try
            {
                feedbackUserEntity.EntityState = EntityState.Changed;
                feedbackUserEntity.Sys_IsDeleted = true;
                if (Update(feedbackUserEntity))
                {
                    IsOperationSuccessfull = true;
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);

                throw;
            }

            return IsOperationSuccessfull;
        }

        public FeedbackUser GetByID(string ID)
        {
            FeedbackUser _SelectedUser = null;
            try
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandType = CommandType.StoredProcedure;
                CmdObj.CommandText = "Ivy_FeedBackUser_GetByID";

                DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackUserKey", ParameterDirection.Input, DbType.Int32, Convert.ToInt32(ID));
                //DataAccessProviderUtility.AddParameter(CmdObj, "@SYS_IsDeleted", ParameterDirection.Input, DbType.Boolean, this.Sys_IsDeleted);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@SYS_OrganizationID", ParameterDirection.Input, DbType.Int32, this.Sys_OrganizationID);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();

                DbDataReader _DataReader = CmdObj.ExecuteReader();
                if (_DataReader.HasRows)
                {
                    if (_DataReader.Read())
                    {
                        _SelectedUser = new FeedbackUser();
                        TranslateUserFieldsFromReaderToEntity(_SelectedUser, _DataReader);

                    }
                }
                else
                {
                    throw new Exception("No FeedbackUser found");
                }
                CmdObj.Dispose();
                DbConn.Close();

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }


            return _SelectedUser;
        }
        public List<FeedbackUser> GetAll()
        {
            var _FeedbackUserList = new List<FeedbackUser>();

            try
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandType = CommandType.StoredProcedure;
                CmdObj.CommandText = "Ivy_FeedbackUser_GetAll";

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();

                DbDataReader _DataReader = CmdObj.ExecuteReader();
                if (_DataReader.HasRows)
                {
                    while (_DataReader.Read())
                    {
                        var lFeedbackUser = new FeedbackUser();
                        this.TranslateUserFieldsFromReaderToEntity(lFeedbackUser, _DataReader);
                        _FeedbackUserList.Add(lFeedbackUser);
                    }
                }
                CmdObj.Dispose();
                DbConn.Dispose();
            }
            catch (Exception ex)
            {

                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }

            return _FeedbackUserList;
        }
        public List<FeedbackUser> GetByCriteria(FeedbackUserCriteria feedbackUserCriteria)
        {
            var _FeedbackUserList = new List<FeedbackUser>();

            try
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Ivy_FeedbackUser_GetByCriteria";
                CmdObj.CommandType = CommandType.StoredProcedure;

                DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackUserName", ParameterDirection.Input, DbType.String, feedbackUserCriteria.FeedbackUserName);
                DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackUserEmail", ParameterDirection.Input, DbType.String, feedbackUserCriteria.FeedbackUserEmail);
                DataAccessProviderUtility.AddParameter(CmdObj, "@RegisteredUserKey", ParameterDirection.Input, DbType.Int32, feedbackUserCriteria.RegisteredUserkey);

                DatabaseCommon.DefaultStoredProcParameterValuesForGetByCriteria(CmdObj, feedbackUserCriteria);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();

                DbDataReader _DataReader = CmdObj.ExecuteReader();
                if (_DataReader.HasRows)
                {
                    while (_DataReader.Read())
                    {
                        FeedbackUser _FeedbackUserTemp = new FeedbackUser();
                        this.TranslateUserFieldsFromReaderToEntity(_FeedbackUserTemp, _DataReader);

                        _FeedbackUserList.Add(_FeedbackUserTemp);
                    }

                }
                CmdObj.Dispose();
                DbConn.Dispose();
            }
            catch (Exception ex)
            {

                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }

            return _FeedbackUserList;
        }

        #endregion  IDaoInterface<FeedbackUser,Feedbackcserriteria> Members
        public FeedbackUser GetByEmail(string email)
        {
            FeedbackUser SelectedUser = null;
            try
            {

                DbConnection dbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Ivy_FeedbackUser_GetByEmail";
                CmdObj.CommandType = CommandType.StoredProcedure;

                DataAccessProviderUtility.AddParameter(CmdObj, "@FeedbackUserEmail", ParameterDirection.Input, DbType.String, email);
                CmdObj.Connection = dbConn;
                dbConn.Open();
                CmdObj.Prepare();
                DbDataReader _DataReader = CmdObj.ExecuteReader();
                if (_DataReader.HasRows)
                {
                    if (_DataReader.Read())
                    {
                        SelectedUser = new FeedbackUser();
                        TranslateUserFieldsFromReaderToEntity(SelectedUser, _DataReader);
                        
                    }
                }
            }
            catch (Exception ex)
            {
                
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }


            return SelectedUser;
        }


        #endregion Public Methods

        #region events

        #endregion evets

        #region Private Methods
        private void TranslateUserFieldsFromReaderToEntity(FeedbackUser FeedbackUser, DbDataReader DataReader)
        {
            try
            {
                FeedbackUser.FeedbackUserKey = Convert.ToInt32(DataReader["FeedbackUserKey"]);
                FeedbackUser.FeedbackUserName = Convert.ToString(DataReader["FeedbackUserName"]);
                FeedbackUser.FeedbackUserEmail = Convert.ToString(DataReader["FeedBackUserEmail"]);
                FeedbackUser.RegisteredUserkey = Convert.ToInt32(DataReader["RegisteredUserKey"]);

                DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(FeedbackUser, DataReader);
            }
            catch (Exception)
            {

                throw;
            }
        }

        //private void CopyValueFromEntityToADO(FeedbackUser SelectedUser)
        //{
        //    this.FeedbackUserKey = SelectedUser.FeedbackUserKey;
        //    this.FeedbackUserName = SelectedUser.FeedbackUserName;
        //    this.FeedbackUserEmail = SelectedUser.FeedbackUserEmail;
        //    this.RegisteredUserkey = SelectedUser.RegisteredUserkey;
        //    this.Sys_CreatedByID = SelectedUser.Sys_CreatedByID;
        //    this.Sys_DateOfCreation = SelectedUser.Sys_DateOfCreation;
        //    this.Sys_DeleteType = SelectedUser.Sys_DeleteType;
        //    this.Sys_InitialPrimaryKeys = SelectedUser.Sys_InitialPrimaryKeys;
        //    this.Sys_IsDeleted = SelectedUser.Sys_IsDeleted;
        //    this.Sys_ModifiedByID = SelectedUser.Sys_ModifiedByID;
        //    this.Sys_OrganizationID = SelectedUser.Sys_OrganizationID;
        //    this.Sys_WorkstationName = SelectedUser.Sys_WorkstationName;
        //    this.EntityState = SelectedUser.EntityState;
        //}
        #endregion Private Methods

    }
}
